package com.ftg.learn.jdbc.chapter01.dao;

import com.ftg.learn.jdbc.chapter01.entity.Dept;
import com.ftg.learn.jdbc.chapter01.util.DaoUtil;
import com.ftg.learn.jdbc.chapter01.vo.EmpAndDept;

import java.util.List;

/**
 * 这个类的叫开发人员
 * dao :我里面的类全是用来操作sql
 * DeptDao  操作dept
 *
 * @author kangjx
 * @createTime 2022/9/23
 */
public class DeptDao extends DaoUtil<Dept> {


    /**
     * 查询部门表的全部信息
     *
     * @return
     */
    public List<Dept> showAllDeptInfo() {
        String sql = "select * from dept";
        return super.queryForClass(sql);
    }

    /**
     * 查询部门表的全部信息
     *
     * @return
     */
    public List<Object> showAllDeptInfo1() {
        String sql = "select " +
                "eno, ename, job, mgr, hirdate, sal, comm, emp.deptno, dname, loc " +
                "from emp left join dept on emp.deptno = dept.deptno";
        return super.queryForClass(EmpAndDept.class, sql);
    }

    /**
     * 测试 json转换方法
     *
     * @return
     */
    public String demoQueryForListToJson() {
        String sql = "select * from dept";
        return super.queryForListToJson(sql);
    }

    public String demoQueryForClassToJson() {
        String sql = "select * from dept";
        return super.queryForClassToJson(sql);
    }

    public String demoQueryForPojoToJson() {
        String sql = "select ename,eno,job,emp.deptno,dname,loc from emp left join dept on emp.deptno = dept.deptno " +
                "where dname like concat(?,'%')";
        return super.queryForPojoToJson(EmpAndDept.class, sql, "A");
    }

    /**
     * dept表的添加操作
     * @return int
     */
    public int saveDept(int deptno,String dname,String loc){
        String sql = "insert into dept(deptno,dname,loc) values(?,?,?)";
        return super.doExecuteUpdate(sql,deptno,dname,loc);
    }

    public int save(Dept dept){
        return super.saveObject(dept);
    }

    /**
     * 为了测试executeUpdate功能，我们编写一个按主键方法
     * @return
     */
    public List<Dept> findById(int deptno){
        String sql = "select deptno,dname,loc from dept where deptno = ?";
        return super.queryForClass(sql,deptno);
    }


    /**
     * 正常删除操作，通过sql直接来完成
     * @return
     */
    public int deleteDeptByid(int deptno){
        String sql = "delete from dept where deptno = ?";
        return super.doExecuteUpdate(sql,deptno);
    }

    /**
     * 正常删除操作，通过sql直接来完成
     * @return
     */
    public int deptDelById(int deptno){
        return super.deleteById(deptno);
    }

    public int deptDelByPara(int deptno){
        return super.deleteByPara(()->"where deptno > ?",deptno);
    }

    public int updateSql(){
        String sql = "update dept set dname=?,loc = ? where deptno = ?";
        return super.doExecuteUpdate(sql,"软研一","哈尔滨");
    }

    /**
     * 批量删除操作
     * @param deptno
     * @return
     */
    public boolean deleteByDeptWithId(int deptno){
        String[] sqls = {"update emp set deptno = null where deptno = ?","delete from dept where deptno = ?"};
        return super.doBathDml(sqls,new Object[]{10},new Object[]{10});
    }



//    解决个问题
//    如果说表名与类名不相同如何处理，列名与字段不相同如何处理

    public static void main(String[] args) {
        DeptDao d = new DeptDao();
        System.out.println(d.deleteByDeptWithId(10));
    }

}
